﻿using Common.Entity;
using Common.SqlSugar;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp1.控件
{
    public partial class PLCErrorCodeContorl : UIUserControl
    {
        public PLCErrorCodeContorl()
        {
            InitializeComponent();
            ContorlGenerateHelper.SetGridPropertyAndName(uiDataGridView1, typeof(PLCErrorCode));
            SearchAndBind();
        }

        private async void btnAdd_Click(object sender, EventArgs e)
        {
            try
            {
                int code = tbxCode.Text.ToInt();
                string info = tbxInfo.Text.ToString();
                PLCErrorCode pLCErrorCode = new PLCErrorCode() { Code = code, Info = info };
                await SqlSugarHelper.Instance.Db.Insertable(pLCErrorCode).ExecuteCommandAsync();
                SearchAndBind();
            }
            catch (Exception ex)
            {
                UIMessageBox.Show(ex.Message);
            }
        }
        private async void SearchAndBind()
        {
            if (!DesignMode&&IsHandleCreated)
            {

                List<PLCErrorCode> plcError = await SqlSugarHelper.Instance.Db.Queryable<PLCErrorCode>().ToListAsync();

                uiDataGridView1.DataSource = plcError;
            }
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            SearchAndBind();
        }

        private async void btnUpdate_Click(object sender, EventArgs e)
        {
            try
            {
                List<PLCErrorCode> plcError = uiDataGridView1.DataSource as List<PLCErrorCode>;
               await SqlSugarHelper.Instance.Db.Updateable<PLCErrorCode>(plcError).ExecuteCommandAsync();
            }
            catch (Exception ex)
            {
                UIMessageBox.Show(ex.Message);
            }
        }
    }
}
